package controller.billing;

import java.util.List;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

import model.Belegung;
import model.Kunde;
import model.Reservierung;

public class ReservationTableModel extends AbstractTableModel {


	public final String[] HeaderNames = { "Customers", "Arrival", "Departure", "Rooms", "Paid"};
	private List<Reservierung> reservations;

	public ReservationTableModel() {
		
	}

	@Override
	public String getColumnName(int column) {
		return HeaderNames[column];
	};

	@Override
	public int getRowCount() {
		return reservations.size();
	}

	@Override
	public int getColumnCount() {
		return HeaderNames.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Reservierung z = reservations.get(rowIndex);
		switch (columnIndex) {
		
		case 0: // customernames
			String customerNames="";
			List<Belegung> belegungen = z.getBelegungen();
			for(Belegung b : belegungen){
				List<Kunde> kunden = b.getKunden();
				
				for(Kunde k : kunden){
					customerNames += k.getNachname()+", ";
				}
			}
			
			return trimEndingCommaSpace(customerNames);
			
		case 1: // from
			return z.getVon();
			
		case 2: // bis
			return z.getBis();
		
		case 3: // room names
			String roomNames="";
			for(Belegung b : z.getBelegungen()){
				roomNames+=b.getZimmer().getName()+", ";
			}
			
			return trimEndingCommaSpace(roomNames);
			
		case 4: // has bill?
			return z.getRechnung() != null;
		}
		return "Error";
	}
	
	@Override
	public java.lang.Class<?> getColumnClass(int columnIndex) {
		try{
			return this.getValueAt(0, columnIndex).getClass();
		}catch(Exception e){
			return super.getColumnClass(columnIndex);
		}
		
	};
	
	public void setReservations(List<Reservierung> reservations){
		this.reservations = reservations;
	}

	public List<Reservierung> getReservations() {
		return reservations;
	}
	
	private String trimEndingCommaSpace(String string){
		// trim ending ", "
		if(string.length() > 0){
			string = string.substring(0, string.length()-2);
		}
		
		return string;
	}
	
}
